const Controller = require('egg').Controller;

class AddressController extends Controller {
    async getList() {
        const {ctx,app} = this;
        try{
            const data = await ctx.service.address.getList()
            ctx.body = data;
        } catch(err){
            ctx.body = {
                code:-1,
                message:err.message
            }
        }
    }
    async addList() {
        const {ctx,app} = this;
        try{
            ctx.validate({
                title: "string",
                content: "string",
                phone: "string"
            }, ctx.request.body)
            const data = await ctx.service.address.addList()
            if(data.affectedRows && data.affectedRows>=1) {
                ctx.body = {
                    code:1,
                    message:'添加成功'
                }
            } else {
                ctx.body = {
                    code:-1,
                    message:'添加失败'
                }
            }
        } catch(err){
            ctx.body = {
                code:-1,
                message:err.message
            }
        }
    }
    async editList() {
        const {ctx,app} = this;
        try{
            ctx.validate({
                title: "string",
                content: "string",
                phone: "string",
                id: "number",
            }, ctx.request.body)
            const data = await ctx.service.address.editList()
            if(data.affectedRows && data.affectedRows>=1) {
                ctx.body = {
                    code:1,
                    message:'编辑成功'
                }
            } else {
                ctx.body = {
                    code:-1,
                    message:'编辑失败'
                }
            }
        } catch(err){
            ctx.body = {
                code:-1,
                message:err.message
            }
        }
    }
    async delList() {
        const {ctx,app} = this;
        try{
            ctx.validate({
                id: "string",
            }, ctx.params)
            const data = await ctx.service.address.delList()
            if(data.affectedRows && data.affectedRows>=1) {
                ctx.body = {
                    code:1,
                    message:'删除成功'
                }
            } else {
                ctx.body = {
                    code:-1,
                    message:'删除失败'
                }
            }
        } catch(err){
            ctx.body = {
                code:-1,
                message:err.message
            }
        }
    }
}

module.exports = AddressController;
